<template>
  <!-- 报价管理 -->
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="add"
          v-auth-button="['add/quoteManagement']"
        ></el-button>
      </div>
      <div class="right-panel">
        <div class="right-panel-search">
          <el-input
            v-model="searchValue.search"
            placeholder="报价名称"
            clearable
          ></el-input>
          <el-select
            v-model="searchValue.status"
            placeholder="请选择报价状态"
            style="width: 100%"
            clearable
          >
            <el-option label="待审核" :value="0" />
            <el-option label="已通过" :value="1" />
            <el-option label="未通过" :value="2" />
            <el-option label="审核中" :value="3" />
          </el-select>
          <el-select
            v-if="userList.length != 0"
            v-model="searchValue.recodeId"
            placeholder="请选择报价人"
            style="width: 100%"
            clearable
          >
            <el-option
              v-for="item in userList"
              :key="item.id"
              :label="item.username"
              :value="item.id"
            />
          </el-select>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="upsearch"
          ></el-button>
        </div>
      </div>
    </el-header>
    <el-main class="nopadding">
      <scTable
        ref="table"
        :apiObj="apiObj"
        :params="params"
        row-key="id"
        dataType="listSortBydelete"
        @row-dblclick="handleRowDblclick"
        stripe
      >
        <el-table-column
          label="ID"
          prop="id"
          width="70"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <!-- <el-table-column
          label="报价单号"
          prop="quotesNumber"
          width="250"
        ></el-table-column> -->
        <el-table-column
          label="报价名称"
          :show-overflow-tooltip="true"
          prop="quotesName"
          min-width="250"
        >
        </el-table-column>
        <!-- <el-table-column label="合同名称" prop="contractName" min-width="200">
        </el-table-column> -->
        <el-table-column label="报价总额" prop="price" width="100">
          <template #default="scope">
            <p>{{ scope.row.price }}元</p>
          </template>
        </el-table-column>
        <el-table-column
          label="报价日期"
          prop="createTime"
          width="100"
        ></el-table-column>
        <el-table-column label="付款方式" prop="payDescription" min-width="100">
        </el-table-column>
        <el-table-column label="交货日期" prop="deliveryTime" width="120">
          <template #default="scope">
            <p>{{ scope.row.deliveryTime }}天之后</p>
          </template>
        </el-table-column>
        <el-table-column
          label="验收标准"
          prop="checkRule"
          :show-overflow-tooltip="true"
          min-width="120"
        >
        </el-table-column>
        <el-table-column
          label="描述"
          prop="description"
          :show-overflow-tooltip="true"
          min-width="120"
        >
        </el-table-column>
        <el-table-column
          label="报价人"
          fixed="right"
          prop="adminName"
          width="100"
        >
        </el-table-column>
        <el-table-column
          label="审核状态"
          prop="status"
          width="100"
          fixed="right"
        >
          <template #default="scope">
            <!-- 0未审核 1通过 2未通过 3审核中 -->
            <p v-if="scope.row.status == 0">待审核</p>
            <p v-if="scope.row.status == 1" style="color: #67c23a">通过</p>
            <p v-if="scope.row.status == 2" style="color: #f56c6c">未通过</p>
            <p v-if="scope.row.status == 3" style="color: #ff9b31">审核中</p>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="300">
          <template #default="scope">
            <el-button-group>
              <el-button
                text
                type="primary"
                size="small"
                @click="table_apply(scope.row, scope.$index)"
                v-if="
                  scope.row.status != 0 && scope.row.recodeId == userInfo.userId
                "
                v-auth-button="['apply/quoteManagement']"
                >申请修改</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="printPreview(scope.row)"
                >打印预览</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="addContract(scope.row)"
                v-auth-button="['add/quoteManagement']"
                v-if="scope.row.status == 1 && scope.row.isContract == 0"
                >添加合同</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_applyLog(scope.row, scope.$index)"
                v-auth-button="['applyLog/quoteManagement']"
                >历史记录</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_show(scope.row, scope.$index)"
                v-auth-button="['show/quoteManagement']"
                >查看</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_edit(scope.row, scope.$index)"
                v-auth-button="['edit/quoteManagement']"
                v-if="scope.row.status == 0"
                >编辑</el-button
              >
              <el-popconfirm
                title="确定作废吗？"
                @confirm="table_del(scope.row, scope.$index)"
                v-if="scope.row.status != 0"
              >
                <template #reference>
                  <el-button
                    text
                    type="primary"
                    size="small"
                    v-auth-button="['scrap/quoteManagement']"
                    >作废</el-button
                  >
                </template>
              </el-popconfirm>
              <el-popconfirm
                title="确定删除吗？"
                @confirm="table_del(scope.row, scope.$index)"
                v-if="scope.row.status == 0"
              >
                <template #reference>
                  <el-button
                    text
                    type="primary"
                    size="small"
                    v-auth-button="['delete/quoteManagement']"
                    >删除</el-button
                  >
                </template>
              </el-popconfirm>
            </el-button-group>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>

  <!-- 申请修改 -->
  <apply-dialog
    v-if="dialog.apply"
    ref="applyDialog"
    @success="handleSaveSuccess"
    @closed="dialog.apply = false"
  ></apply-dialog>

  <!-- 历史记录 -->
  <log-dialog
    v-if="dialog.log"
    ref="logDialog"
    @success="handleSaveSuccess"
    @closed="dialog.log = false"
  ></log-dialog>

  <print-dom ref="printDom" v-show="false" id="printDom"></print-dom>
</template>

<script>
import applyDialog from "@/components/applyDialog"
import logDialog from "./components/logDialog.vue"
import print from "@/utils/print"
import printDom from "./components/printDom"
export default {
  name: "quoteManagement",
  components: {
    printDom,
    applyDialog,
    logDialog,
  },
  data () {
    return {
      dialog: {
        save: false,
        permission: false,
        apply: false,
        log: false,
      },
      apiObj: this.$API.contract.quoteManagement.getQuotes,
      params: {
        status: 0
      },
      userList: [],
      searchValue: {
        search: null,
        recodeId: "",
        status: 0
      },
      userInfo: {},
    }
  },
  mounted () {
    this.userInfo = this.$TOOL.data.get("USER_INFO")
    this.getUserList()
  },
  methods: {
    //打印预览
    printPreview (row) {
      this.getDetail(row.id)
    },
    // 获取报价详情
    async getDetail (id) {
      let { data: configData } =
        await this.$API.contract.contractConfig.getConfigDetail.post({
          id: 1,
        })
      let { data } =
        await this.$API.contract.quoteManagement.getQuoteDetailById.post(
          { id }
        )
      this.$refs.printDom.provideForm = data
      this.$refs.printDom.needForm = configData
      this.$nextTick(() => {
        let dom = document.querySelector("#printDom")
        let clone = dom.cloneNode(true)
        clone.style.display = "block"
        print(clone)
      })
    },

    // 申请修改  -- 重新审核
    table_apply (row) {
      this.dialog.apply = true
      this.$nextTick(() => {
        this.$refs.applyDialog.open(row, 1)
      })
    },

    //添加
    add () {
      this.$router.push({
        path: "/customer/quoteManagement/quoteAdd",
        query: {
          mode: "add",
        },
      })
    },
    //编辑
    table_edit (row) {
      this.$router.push({
        path: "/customer/quoteManagement/quoteAdd",
        query: {
          id: row.id,
          mode: "edit",
        },
      })
    },

    // 历史记录（申请日志）
    table_applyLog (row) {
      this.dialog.log = true
      this.$nextTick(() => {
        this.$refs.logDialog.open(row)
      })
    },

    //查看
    table_show (row) {
      this.$router.push({
        path: "/customer/quoteManagement/quoteDetail",
        query: {
          id: row.id,
        },
      })
    },
    //删除
    async table_del (row) {
      var reqData = { id: row.id }
      var res = await this.$API.contract.quoteManagement.deleteQuote.post(
        reqData
      )
      if (res.code == 200) {
        this.$refs.table.refresh()
        this.$message.success("操作成功")
      }
    },
    //搜索
    upsearch () {
      this.$refs.table.upData(this.searchValue)
    },

    // 添加合同
    addContract (row) {
      this.$router.push({
        path: "/contract/contractManagement/contractAdd",
        query: {
          quoteId: row.id,
          mode: "add",
        },
      })
    },

    // 获取所有用户列表
    async getUserList () {
      let res = await this.$API.user.getAdminList.get()
      this.userList = res.data
    },

    //本地更新数据
    handleSaveSuccess (data, mode) {
      if (mode == "add") {
        // this.$refs.table.refresh()
      } else if (mode == "edit") {
        // this.$refs.table.refresh()
      }
      this.$refs.table.refresh()
    },

    // 双击表格
    handleRowDblclick (row) {
      this.table_show(row)
    },
  },
};
</script>

<style></style>
